package rohyunji;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.GridLayout;

import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Dimension;

import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextArea;
import javax.swing.JTable;

import java.awt.Component;

import javax.swing.JList;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;

import java.awt.CardLayout;

import javax.swing.JDesktopPane;

//<1.CardLayout Code_LoginArea>

public class StartMain extends JFrame {

	private JPanel contentPane;
	private static StartMain frame = null;
	private JTable table;
	private JTextField textField;
	private JTextField login_id;
	private JPasswordField login_pw;
	private JTextField login_Id;
	private JPasswordField loginpw;
	private String l_type;
	private JTextField textField_1;
	private int result2 = 0;
	private String result = "", mb_no = "";
	private CardLayout loginAreaCard;
	private CardLayout menu2AreaCard;
	private final CardLayout rightCard;


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new StartMain();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	// 프레임만들기 시작(생성자에서)
	public StartMain() {
	
		
		setTitle("Start_Main");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 80, 1000, 500);

		// 메뉴바 생성
		final JMenuBar mainMnbar = new JMenuBar();
		setJMenuBar(mainMnbar);

		JMenu mnFile = new JMenu("파일(F)");
		JMenuItem mntmLogin = new JMenuItem("로그인(L)");
		JMenu mnNewMenu_1 = new JMenu("도움말(H)");
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("프로그램 서비스 안내(I)");

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("종료(X)");

		mainMnbar.add(mnFile);
		mnFile.add(mntmLogin);
		mnFile.add(mntmNewMenuItem_1);

		mainMnbar.add(mnNewMenu_1);
		mnNewMenu_1.add(mntmNewMenuItem_2);

		JMenuItem mntmNewMenuItem = new JMenuItem("프로그램정보(A)");
		mnNewMenu_1.add(mntmNewMenuItem);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		final JPanel panel_3 = new JPanel();
		panel_3.setBackground(Color.WHITE);
		panel_3.setAlignmentY(Component.TOP_ALIGNMENT);
		panel_3.setAlignmentX(Component.LEFT_ALIGNMENT);
		contentPane.add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new BorderLayout(0, 0));

		JPanel searchArea = new JPanel();
		searchArea.setBackground(Color.WHITE);
		panel_3.add(searchArea, BorderLayout.NORTH);

		JLabel label = new JLabel("공고검색");
		searchArea.add(label);

		textField_1 = new JTextField();
		searchArea.add(textField_1);
		textField_1.setColumns(20);

		JButton btnNewButton_3 = new JButton("Search");
		searchArea.add(btnNewButton_3);

		rightCard = new CardLayout();

		final JPanel right_area = new JPanel();
		panel_3.add(right_area, BorderLayout.EAST);
		right_area.setLayout(rightCard);

		JPanel panel_4 = new JPanel();
		panel_4.setBackground(Color.WHITE);
		right_area.add(panel_4, "rightcard2");

		// loginP.setLayout(gbl_loginP);
		// loginArea.add(loginP, "loginP");

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setPreferredSize(new Dimension(670, 300));
		panel_4.add(scrollPane);

		DefaultTableModel model = new DefaultTableModel() {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			} // 열 수정 불가
		};

		String title[] = { "회사명", "공고제목", "지원학력", "지원경력", "고용형태", "근무지역",
				"연봉조건", "등록일", "마감일" };
		model.setColumnIdentifiers(title);
		for (int i = 1; i <= 20; ++i) {
			model.addRow(new String[] { i + 1 + "", i + 2 + "", i + 3 + "",
					i + 4 + "", i + 5 + "", i + 6 + "", i + 7 + "", i + 8 + "",
					i + 9 + "" });
		}
		table = new JTable();
		table.setModel(model);

		DefaultTableCellRenderer cell = new DefaultTableCellRenderer();
		cell.setHorizontalAlignment(JLabel.CENTER); // 가운데 정렬
		table.getColumnModel().getColumn(0).setCellRenderer(cell);// 회사명

		table.getColumnModel().getColumn(1).setCellRenderer(cell); // 공고제목
		table.getColumnModel().getColumn(1).setPreferredWidth(152);

		table.getColumnModel().getColumn(2).setCellRenderer(cell);//
		table.getColumnModel().getColumn(2).setPreferredWidth(52);

		table.getColumnModel().getColumn(3).setCellRenderer(cell);
		table.getColumnModel().getColumn(4).setCellRenderer(cell);

		scrollPane.setViewportView(table);

		JPanel left_area = new JPanel();
		panel_3.add(left_area, BorderLayout.WEST);
		left_area.setLayout(new BorderLayout(0, 0));

		loginAreaCard = new CardLayout();

		// <1.CardLayout Code_LoginArea>
		
		final JPanel loginArea = new JPanel();
		left_area.add(loginArea, BorderLayout.NORTH);
		loginArea.setAlignmentY(Component.TOP_ALIGNMENT);
		loginArea.setSize(50, 50);
		loginArea.setLayout(loginAreaCard);

		JPanel loginP = new JPanel();
		loginP.setBackground(Color.WHITE);
		GridBagLayout gbl_loginP = new GridBagLayout();
		gbl_loginP.columnWidths = new int[] { 30, 25, 15 };
		gbl_loginP.rowHeights = new int[] { 23, 0 };
		gbl_loginP.columnWeights = new double[] { 0.0, 0.0, 0.0 };
		gbl_loginP.rowWeights = new double[] { 0.0, 0.0 };
		loginP.setLayout(gbl_loginP);
		loginArea.add(loginP, "loginP");

		final JLabel lblLoginid = new JLabel("아이디");
		GridBagConstraints gbc_lblLoginid = new GridBagConstraints();
		gbc_lblLoginid.anchor = GridBagConstraints.EAST;
		gbc_lblLoginid.insets = new Insets(0, 0, 5, 5);
		gbc_lblLoginid.gridx = 0;
		gbc_lblLoginid.gridy = 0;
		loginP.add(lblLoginid, gbc_lblLoginid);

		final JTextField login_Id_1 = new JTextField();
		GridBagConstraints gbc_login_Id_1 = new GridBagConstraints();
		gbc_login_Id_1.insets = new Insets(0, 0, 5, 5);
		gbc_login_Id_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_login_Id_1.gridx = 1;
		gbc_login_Id_1.gridy = 0;
		loginP.add(login_Id_1, gbc_login_Id_1);
		login_Id_1.setColumns(10);

		final JLabel lblpwd = new JLabel("패스워드");
		GridBagConstraints gbc_lblpwd = new GridBagConstraints();
		gbc_lblpwd.anchor = GridBagConstraints.EAST;
		gbc_lblpwd.insets = new Insets(0, 0, 5, 5);
		gbc_lblpwd.gridx = 0;
		gbc_lblpwd.gridy = 1;
		loginP.add(lblpwd, gbc_lblpwd);

		final JPasswordField loginpw_1 = new JPasswordField();
		GridBagConstraints gbc_loginpw_1 = new GridBagConstraints();
		gbc_loginpw_1.insets = new Insets(0, 0, 5, 5);
		gbc_loginpw_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_loginpw_1.gridx = 1;
		gbc_loginpw_1.gridy = 1;
		loginP.add(loginpw_1, gbc_loginpw_1);

		JButton btn1 = new JButton("로그인");
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.fill = GridBagConstraints.BOTH;
		gbc_btnNewButton.gridwidth = 2;
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton.gridx = 0;
		gbc_btnNewButton.gridy = 2;
		loginP.add(btn1, gbc_btnNewButton);

		// 로그인영역 아래 버튼메뉴영역

		menu2AreaCard = new CardLayout();
		final JPanel menuArea2 = new JPanel();
		menuArea2.setBackground(Color.WHITE);
		left_area.add(menuArea2, BorderLayout.CENTER);
		GridBagLayout gbl_loginArea = new GridBagLayout();
		menuArea2.setLayout(menu2AreaCard);

		// 로그인전 버튼메뉴 영역
		JPanel menuP = new JPanel();
		menuP.setBackground(Color.WHITE);
		loginP.setBackground(Color.WHITE);

		menuArea2.add(menuP, "menuP");
		GridBagLayout gbl_menuP = new GridBagLayout();
		gbl_menuP.columnWidths = new int[] { 10, 8, 0, 0 };
		gbl_menuP.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gbl_menuP.columnWeights = new double[] { 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_menuP.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0 };
		menuP.setLayout(gbl_menuP);

		JLabel lblNewLabel = new JLabel("[채용정보]");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.gridwidth = 2;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 0;
		menuP.add(lblNewLabel, gbc_lblNewLabel);

		JComboBox comboBox = new JComboBox();
		comboBox.setMinimumSize(new Dimension(20, 21));
		comboBox.setPreferredSize(new Dimension(20, 21));
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 1;
		menuP.add(comboBox, gbc_comboBox);
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "전체직종",
				"경영/사무", "마케팅/무역/유통", "영업/고객상담", "IT/인터넷", "연구개발/설계", "생산/제조",
				"전문/특수직", "디자인", "미디어" }));

		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setMinimumSize(new Dimension(20, 21));
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox_1.gridx = 2;
		gbc_comboBox_1.gridy = 1;
		menuP.add(comboBox_1, gbc_comboBox_1);
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] { "전체지역",
				"서울", "경기", "인천", "대전", "세종", "충북", "충남", "광주", "전북", "전남",
				"대구", "울산", "부산", "경북", "경남", "강원", "제주" }));

		JComboBox comboBox_2 = new JComboBox();
		comboBox_2.setMinimumSize(new Dimension(20, 21));
		comboBox_2.setPreferredSize(new Dimension(20, 21));
		GridBagConstraints gbc_comboBox_2 = new GridBagConstraints();
		gbc_comboBox_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_2.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_2.gridx = 1;
		gbc_comboBox_2.gridy = 2;
		menuP.add(comboBox_2, gbc_comboBox_2);
		comboBox_2.setModel(new DefaultComboBoxModel(new String[] { "정규직",
				"계약직", "인턴직" }));

		JComboBox comboBox_3 = new JComboBox();
		comboBox_3.setModel(new DefaultComboBoxModel(new String[] { "모집학력",
				"박사", "석사", "대졸", "전졸", "고졸", "학력무관" }));
		comboBox_3.setMinimumSize(new Dimension(20, 21));
		GridBagConstraints gbc_comboBox_3 = new GridBagConstraints();
		gbc_comboBox_3.insets = new Insets(0, 0, 5, 0);
		gbc_comboBox_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_3.gridx = 2;
		gbc_comboBox_3.gridy = 2;
		menuP.add(comboBox_3, gbc_comboBox_3);

		JButton btnNewButton_4 = new JButton("전체 채용정보");
		GridBagConstraints gbc_btnNewButton_4 = new GridBagConstraints();
		gbc_btnNewButton_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNewButton_4.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton_4.gridx = 1;
		gbc_btnNewButton_4.gridy = 4;
		menuP.add(btnNewButton_4, gbc_btnNewButton_4);

		JButton btnNewButton_7 = new JButton("경력순");
		GridBagConstraints gbc_btnNewButton_7 = new GridBagConstraints();
		gbc_btnNewButton_7.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton_7.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNewButton_7.gridx = 2;
		gbc_btnNewButton_7.gridy = 4;
		menuP.add(btnNewButton_7, gbc_btnNewButton_7);

		JButton btnNewButton_5 = new JButton("등록일순");
		GridBagConstraints gbc_btnNewButton_5 = new GridBagConstraints();
		gbc_btnNewButton_5.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton_5.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNewButton_5.gridx = 1;
		gbc_btnNewButton_5.gridy = 5;
		menuP.add(btnNewButton_5, gbc_btnNewButton_5);

		JButton btnNewButton_6 = new JButton("마감일순");
		GridBagConstraints gbc_btnNewButton_6 = new GridBagConstraints();
		gbc_btnNewButton_6.insets = new Insets(0, 0, 5, 0);
		gbc_btnNewButton_6.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnNewButton_6.gridx = 2;
		gbc_btnNewButton_6.gridy = 5;
		menuP.add(btnNewButton_6, gbc_btnNewButton_6);

		// 로그인버튼처리

		btn1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				LoginSVC lsvc = new LoginSVC();
				String id = login_Id_1.getText();
				String pw = new String(loginpw_1.getPassword());
				result = lsvc.Login(id, pw);
				if (id.length() > 1 && pw.length() > 1) {
					String[] buttons = { "확인", "취소" };
					result2 = JOptionPane.showOptionDialog(null, result,
							"Login", 0, JOptionPane.INFORMATION_MESSAGE, null,
							buttons, buttons[0]);

					
					
					
					
					if (result2 == 0 && result == "로그인합니다.") // 로그인이 되었고 확인눌렀을때
					{
						// 1. Login 한후의 LoginArea 영역 패널바뀜
						JPanel loginAfter = new LoginSVC(login_Id_1.getText());

						JButton logout_btn2 = new JButton("로그아웃2");

						GridBagConstraints gbc_logout_btn2 = new GridBagConstraints();
						gbc_logout_btn2.insets = new Insets(0, 0, 0, 5);
						gbc_logout_btn2.gridx = 2;
						gbc_logout_btn2.gridy = 2;
						loginAfter.add(logout_btn2, gbc_logout_btn2);
						loginAfter.setBackground(Color.white);
						loginArea.add(loginAfter, "loginAfter");
						loginAreaCard.show(loginArea, "loginAfter");
						logout_btn2.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {

								// 로그아웃 처리시 다시 로그인전으로 되돌림

								loginAreaCard.show(loginArea, "loginP");
								menu2AreaCard.show(menuArea2, "menuP");
								rightCard.show(right_area, "rightcard2");
								setJMenuBar(mainMnbar);

							}
						});

						login_Id_1.setText("");
						loginpw_1.setText("");

						final String mb_no = lsvc.selectMb_no(id);
						String mb_type = lsvc.selectMb_type(id);
						System.out.println("mb_no=" + mb_no);

						// 로그인후 메뉴바뀜

						if (mb_type.equals("C")) 
						{
							
				
							JMenuBar jmb = new JMenuBar();
							JMenu cp_Menu = new JMenu("기업회원");
							JMenuItem cpManage = new JMenuItem("기업회원정보관리");
							JMenuItem mbManage = new JMenuItem("회원정보관리");
							JMenuItem ac_create = new JMenuItem("채용공고관리");
							JMenuItem re_state = new JMenuItem("채용현황관리");
							JMenuItem re_search = new JMenuItem("채용인재검색");
							jmb.add(cp_Menu);
							cp_Menu.add(cpManage);
							cp_Menu.add(mbManage);
							cp_Menu.add(ac_create);
							cp_Menu.add(re_state);
							cp_Menu.add(re_search);

							// 채용공고관리 선택시
							ac_create.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent e) {
									// ,rightCard,right_area
									JPanel ac_manager = new AnnounceSVC(
											menu2AreaCard, menuArea2,
											rightCard, right_area);
									ac_manager.setBackground(Color.white);

									menuArea2.add(ac_manager, "ac_manager");
									menu2AreaCard.show(menuArea2, "ac_manager");// menu2AreaCard영역바뀜

								}
							});
							
							// 기업정보관리 클릭했을때
							cpManage.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent e) {
									//
									JPanel cp_manager=new CompanySVC(mb_no,menu2AreaCard, menuArea2,rightCard,right_area);
									cp_manager.setBackground(Color.white);
									
									menuArea2.add(cp_manager,"cp_manager");
									menu2AreaCard.show(menuArea2, "cp_manager");//menu2AreaCard영역바뀜
						
								}
							});


				
							JMenu hp_Menu = new JMenu("기업회원");
							JMenuItem hp_jobsinfo = new JMenuItem("프로그램정보");
							hp_Menu.add(hp_jobsinfo);

							setJMenuBar(jmb);
							setVisible(true);
						}

						else if (mb_type.equals("N")) {
							JMenuBar jmb2 = new JMenuBar();
							JMenu mnNewMenu = new JMenu("개인회원");
							jmb2.add(mnNewMenu);
							JMenuItem menuItem = new JMenuItem("ㅋㅋㅋ");
							mnNewMenu.add(menuItem);
							setJMenuBar(jmb2);

						} else if (mb_type.equals("A")) {
							JMenuBar jmb3 = new JMenuBar();
							JMenu mnNewMenu = new JMenu("관리자");
							jmb3.add(mnNewMenu);

							JMenuItem menuItem = new JMenuItem("ㅋㅋㅋ");
							mnNewMenu.add(menuItem);
							setJMenuBar(jmb3);
						}

					}
					// 로그인 알림후 취소버튼 클릭시 아이디,비밀번호 빈칸처리
					else if (result2 == 1) {
						login_Id_1.setText("");
						loginpw_1.setText("");
					}
				}
			}
		});

		final JButton JoinBtn = new JButton("회원가입");

		GridBagConstraints gbc_JoinBtn = new GridBagConstraints();
		gbc_JoinBtn.insets = new Insets(0, 0, 5, 5);
		gbc_JoinBtn.gridx = 0;
		gbc_JoinBtn.gridy = 3;
		loginP.add(JoinBtn, gbc_JoinBtn);

		final JButton search_idpw = new JButton("ID/PW 찾기");
		GridBagConstraints gbc_search_idpw = new GridBagConstraints();
		gbc_search_idpw.insets = new Insets(0, 0, 5, 5);
		gbc_search_idpw.fill = GridBagConstraints.HORIZONTAL;
		gbc_search_idpw.gridx = 1;
		gbc_search_idpw.gridy = 3;
		loginP.add(search_idpw, gbc_search_idpw);
		
		
		
		
		
		

	}

}
